$(function(){
    $('#keyword').on('keyup',function (){
        const keyword = $(this).val();
        if(keyword!==null&&keyword!==''&&keyword.length!==0){
            $.ajax({
                type   :'GET',
                url    :'http://localhost:8088/orderSearchAjax?keyword='+keyword,
                success:function (list){
                    console.log(keyword);
                    let productListHTML = '';
                    for(let i=0; i<list.length; i++){
                        productListHTML+='<li class=\"productAutoItem\" data-orderid="';
                        productListHTML+=list[i].orderId;
                        productListHTML+='">';
                        productListHTML+=list[i].username;
                        productListHTML+='</li>';
                    }
                    $('#productAutoList').html(productListHTML);
                    $('#productAutoComplete').show();
                },
                error  :function (errorMsg){
                    console.log(errorMsg);
                }
            });
        }else{
            $('#productAutoComplete').hide();
        }

    });

    $(document).on('click','.productAutoItem',function (){
        const itemId = $(this).data('orderid');
        console.log(itemId);
        $('#productAutoComplete').hide();
        $('#keyword').val('');
        window.location.href='http://localhost:8088/searchOrder?keyword='+itemId ;
    });



    var orign = document.getElementById("orign");//这个是第一个界面
    var obj = document.getElementById("orderAdd");//这个是第二个界面
    var objEdit = document.getElementById("orderEdit");//这个是第三个界面
    var confirmButton = document.getElementsByName("orderNew");//这个是新增按钮
    var EditButton = document.querySelectorAll('.orderEditButton');//这个是编辑按钮的集合

    obj.style.display="none";
    objEdit.style.display="none";
    var names='';
    var values='';
    console.log("hello");
    $('#orderNew').on('click',function(){
        console.log("he");
        orign.style.display="none";//隐藏
        obj.style.display="";//显示
    });
    EditButton.forEach(function(element){
        //将每一个Edit进行绑定
        element.onclick=function(){
            console.log("Edit successfully");
            orign.style.display="none";//隐藏
            objEdit.style.display="";//显示
            //获取所选主键结点
            var name = this.getAttribute("name");
            //获取各个属性结点
            var editusername = document.getElementsByName("editusername");
            if (editusername != null){
                console.log('取到');
                editusername.value="ni hao";
                editusername.value="hello";
            }else{
                console.log('未取到');
            }
            var editshipAddress1 = document.getElementsByName("editshipAddress1");
            var editshipAddress2 = document.getElementsByName("editshipAddress2");
            var editshipCity = document.getElementsByName("editshipCity");
            var editshipZip = document.getElementsByName("editshipZip");
            var editshipCountry = document.getElementsByName("editshipCountry");
            var editbillAddr = document.getElementsByName("editbillAddr");
            var editbillCity = document.getElementsByName("editbillCity");
            var edittotalPrice = document.getElementsByName("edittotalPrice");
            var editcreditCard = document.getElementsByName("editcreditCard");
            var editcardType = document.getElementsByName("editcardType");
            console.log(name);
            //接下来要写ajax了
            $.ajax({
                type     :'GET',
                url      :'http://localhost:8088/orderEditAjax?orderid='+name,
                success  :function (data){
                    //取到键值对了
                    console.log(data.username);
                    console.log(data.shipAddress1);
                    console.log(data.shipAddress2);
                    editusername[0].value = data.username;
                    editshipAddress1[0].value = data.shipAddress1;
                    editshipAddress2[0].value = data.shipAddress2;
                    editshipCity[0].value = data.shipCity;
                    editshipZip[0].value = data.shipZip;
                    editshipCountry[0].value = data.shipCountry;
                    editbillAddr[0].value = data.billAddr;
                    editbillCity[0].value = data.billCity;
                    edittotalPrice[0].value = data.totalPrice;
                    editcreditCard[0].value = data.creditCard;
                    editcardType[0].value = data.cardType;


                },
                error    :function (errorMsg){
                    console.log(errorMsg);
                }
            });
        }
    });
});